home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 43.zip / Sources C- WorkDisk V.adf / dos / mkdbldirs.c < prev    next >
C/C++ Source or Header  |  1987-02-16  |  4KB  |  158 lines

  1. /*
  2. * =====================================================================
  3. *
  4. *   Program : MKDBLDIRS or MakeDoubleDirectories.    
  5. *
  6. *   Author : J. Van Houtven.                     Date : Sept 12th 1987.
  7. *
  8. * =====================================================================
  9. */
  10.  
  11.  
  12. #include "libraries/dosextens.h"
  13.  
  14. # define MAX_NR_OF_DOUBLESUBDIRS  100
  15.  
  16. /* Only UnLock() locks that are obtained with Lock() or DupLock() (?) !!!! */
  17.  
  18. extern struct FileLock *CreateDir(), *CurrentDir();
  19.  
  20. struct FileLock *next_lock[MAX_NR_OF_DOUBLESUBDIRS*2+1],
  21.                 *current_lock = 0L,
  22.                 *first_lock;
  23.  
  24. int  i, nr_of_doublesubdirs;
  25.  
  26.  
  27. main(argc,argv)
  28. int argc;
  29. char *argv[];
  30. {
  31.  char  *dirname, *dirname2, *drivename;
  32.  struct FileLock *create_lock;
  33. void die(), CloseALL();
  34.  
  35.  if(argc>1)
  36.   drivename = argv[1];
  37.  else
  38.  {
  39.   puts("MKDBLDIRS v2.0 : (c) 1987 by AMY PRODUCTIONS.\nSpecially developed for [EMD].\n");
  40.   puts("Usage : MKDBLDIRS <drive> <nr_of_doublesubdirs> <subdir1_name> <subdir2_name>\n");
  41.   puts("<drive>               : 'df1:', 'df2:' or 'df3:'");
  42.   puts("<nr_of_doublesubdirs> : INTEGER NUMBER between [1-100], DEFAULT = 10");
  43.   puts("<subdir1_name>        : CHAR STRING, DEFAULT = \"  * BAMIGA SECTOR ONE *  \"");
  44.   puts("<subdir2_name>        : CHAR STRING, DEFAULT = \"  [BS ONE]  \"");
  45.   puts("Note : All subdirs will be automagically protected !\n");
  46.   exit(0);
  47.  } 
  48.  
  49.  if(argc>2)
  50.   {
  51.    nr_of_doublesubdirs = atoi(argv[2]);
  52.    if((nr_of_doublesubdirs <1) || (nr_of_doublesubdirs>MAX_NR_OF_DOUBLESUBDIRS))
  53.    {
  54.     puts("<nr_of_doublesubdirs> must be within [1-100] !\n");
  55.     exit(0);
  56.    }
  57.   }
  58.  else
  59.   nr_of_doublesubdirs = 10;
  60.  
  61.  if(argc>3)
  62.   dirname = argv[3];
  63.  else
  64.   dirname = "  * BAMIGA SECTOR ONE *  ";
  65.  
  66.  if(argc>4)
  67.   dirname2 = argv[4];
  68.  else
  69.   dirname2 = "  [BS ONE]  ";
  70.  
  71.  /* ====== mkdirs routine ======  */
  72.  
  73.  /* init next_lock[] */
  74.  for(i=0;i<=nr_of_doublesubdirs*2;i++) next_lock[i] = 0;
  75.  
  76.  first_lock = CurrentDir(0);
  77.  CurrentDir(first_lock);
  78.  
  79.  /* lock drive df? */
  80.  if( (current_lock = Lock(drivename,ACCESS_WRITE) ) == 0)
  81.  {
  82.   die("Couldn't get a lock on specified drive !\n");
  83.  }
  84.  
  85.  /* next_lock (nl. next_lock[0]) word gelijkgesteld aan de lock op df1: */
  86.  next_lock[0] = current_lock; /* let op next_lock[0] is een copy v.e. pointer
  87.                                  en moet niet ge_unlocked worden */ 
  88.  
  89.  for(i=0;i<nr_of_doublesubdirs*2;i+=2)
  90.  {
  91.   CurrentDir(next_lock[i]);
  92.  
  93.   /* Test if "dirname" already exists ... */
  94.   /* BUG : We test this by trying to obtain a lock on "dirname"
  95.      but "dirname" could still be a normal file ! */
  96.   if((create_lock = Lock(dirname,ACCESS_WRITE)) !=0)
  97.   {
  98.    UnLock(create_lock);
  99.    die("Subdirectory already exists !\n");
  100.   }
  101.  
  102.   if((create_lock = CreateDir(dirname)) == 0)
  103.    die("Couldn't create subdirectory !\n");
  104.   UnLock(create_lock);
  105.  
  106.   SetProtection(dirname,1);
  107.  
  108.   if((next_lock[i+1] = Lock(dirname,ACCESS_WRITE)) == 0)
  109.    die("Couldn't obtain a lock on subdirectory\n");
  110.  
  111.  
  112.   CurrentDir(next_lock[i+1]);
  113.  
  114.   /* Test if "dirname2" already exists ... */
  115.   /* BUG : We test this by trying to obtain a lock on "dirname2"
  116.      but "dirname2" could still be a normal file ! */
  117.   if((create_lock = Lock(dirname2,ACCESS_WRITE)) !=0)
  118.   {
  119.    UnLock(create_lock);
  120.    die("Subdirectory already exists !\n");
  121.   }
  122.  
  123.   if((create_lock = CreateDir(dirname2)) == 0)
  124.    die("Couldn't create subdirectory !\n");
  125.   UnLock(create_lock);
  126.  
  127.   SetProtection(dirname2,1);
  128.  
  129.   if((next_lock[i+2] = Lock(dirname2,ACCESS_WRITE)) == 0)
  130.    die("Couldn't obtain a lock on subdirectory\n");
  131.    
  132.  
  133.  }
  134.  
  135.  CloseALL();
  136.  
  137. } /* ====== end main ====== */
  138.  
  139. void CloseALL()
  140. {
  141.  
  142.  for(i=nr_of_doublesubdirs*2;i>=1;i--) if(next_lock[i]) UnLock(next_lock[i]);
  143.  
  144.  CurrentDir(first_lock);
  145.  
  146.  /* Lock op drive df? */
  147.  if(current_lock) UnLock(current_lock);
  148.  
  149.  
  150. void die(errormsg)
  151. char *errormsg;
  152. {
  153.  puts(errormsg);
  154.  CloseALL();
  155.  exit(0);
  156. }
  157.